/*
 *
 * Project Name: tennis
 * $Id:  UserPlayRelServiceImpl.java 2016-05-13 14:35:02 $
 */
package org.aceor.tennis.service.impl;

import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.aceor.tennis.common.domain.UserPlayRel;
import org.aceor.tennis.common.enums.PlayApplyStatus;
import org.aceor.tennis.common.exception.BusinessRuntimeException;
import org.aceor.tennis.common.exception.CommonRuntimeException;
import org.aceor.tennis.dao.IBaseDAO;
import org.aceor.tennis.dao.IUserPlayRelDAO;
import org.aceor.tennis.service.IUserPlayRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service("UserPlayRelServiceImpl")
public class UserPlayRelServiceImpl extends AbstractBaseService<IBaseDAO<UserPlayRel>, UserPlayRel> implements IUserPlayRelService<UserPlayRel> {
    @Autowired
    private IUserPlayRelDAO userPlayRelDAO;

    public IBaseDAO<UserPlayRel> getDao() {
        return userPlayRelDAO;
    }

    @Override
    public List<UserPlayRel> queryHasSignUpUsers(Long playId) throws BusinessRuntimeException {
        try {
            return findList("playId", playId);
        } catch (Throwable t) {
            throw new BusinessRuntimeException("查询已经报名的用户失败", t);
        }
    }

    @Override
    public Optional<UserPlayRel> queryOneUserPlayRel(Long userId, Long playId) throws BusinessRuntimeException {
        try {
            return Optional.of(queryOne(buildDataMap(userId, playId)));
        } catch (Throwable e) {
            throw new BusinessRuntimeException("根据用户id和球局id查找用户球局关系失败", e);
        }
    }

    @Override
    public Optional<UserPlayRel> signUpOnePlay(Long userId, Long playId) throws BusinessRuntimeException {
        try {
            UserPlayRel userPlayRel = new UserPlayRel();
            userPlayRel.setPlayId(playId);
            userPlayRel.setUserId(userId);
            userPlayRel.setApplyStatus(PlayApplyStatus.UNDO.getValue());
            if (1 == add(userPlayRel)) {
                return Optional.of(userPlayRel);
            }
            return Optional.absent();
        } catch (Throwable e) {
            throw new BusinessRuntimeException("根据用户id和球局id查找用户球局关系失败", e);
        }
    }

    @Override
    public void agreeSignUpOnePlay(Long userId, Long playId) throws BusinessRuntimeException {
        try {
            UserPlayRel userPlayRel = queryOne(buildDataMap(userId, playId));
            if (null == userPlayRel) {
                throw new BusinessRuntimeException("对应用户球局信息不存在");
            }
            if (Objects.equal(userPlayRel.getApplyStatus(), PlayApplyStatus.UNDO.getValue())) {
                userPlayRel.setApplyStatus(PlayApplyStatus.SUCCESS.getValue());
                if (1 != update(userPlayRel)) {
                    throw new BusinessRuntimeException("更新用户球局申请状态为'同意'失败");
                }
            }
        } catch (CommonRuntimeException e) {
            throw e;
        } catch (Throwable t) {
            throw new BusinessRuntimeException("更新用户球局申请状态为'同意'失败", t);
        }
    }

    @Override
    public void disAgreeSignUpOnPlay(Long userId, Long playId) throws BusinessRuntimeException {
        try {
            UserPlayRel userPlayRel = queryOne(buildDataMap(userId, playId));
            if (null == userPlayRel) {
                throw new BusinessRuntimeException("对应用户球局信息不存在");
            }
            if (Objects.equal(userPlayRel.getApplyStatus(), PlayApplyStatus.UNDO.getValue())) {
                userPlayRel.setApplyStatus(PlayApplyStatus.FAILURE.getValue());
                if (1 != update(userPlayRel)) {
                    throw new BusinessRuntimeException("更新用户球局申请状态为'不同意'失败");
                }
            }
        } catch (CommonRuntimeException e) {
            throw e;
        } catch (Throwable t) {
            throw new BusinessRuntimeException("更新用户球局申请状态为'不同意'失败", t);
        }
    }

    @Override
    public List<UserPlayRel> queryHasJoinPlays(Long userId) throws BusinessRuntimeException {
        try {
            return queryList(
                    ImmutableMap
                            .of("userId", userId)
                            .of("applyStatus", PlayApplyStatus.SUCCESS.getValue()));
        } catch (Throwable t) {
            throw new BusinessRuntimeException("查询已经加入的球局", t);
        }
    }

    private Map<String, Object> buildDataMap(Long userId, Long playId) {
        Map<String, Object> data = Maps.newHashMap();
        data.put("userId", userId);
        data.put("playId", playId);
        return data;
    }
}
